import {
  createApp, defineAsyncComponent
} from 'vue'
import App from './App.vue'
import widget from './widgets/index.js'
import $ from './dfish.js'

$.config({
  debug: true, // 设置为true, 按 Alt + 鼠标左键 看view地址
  templateDir: 'components/',
  preloadDir: 'preload/',
  autoPlaceholder: true,
  downloadTarget: '_blank',
  http: {
    baseURL: 'http://localhost/dfish/webapp/',
    headers: {
      // 'x-request-with': 'dfish',
      // common: {
      //   Authorization: 'sdfew'
      // },
      post: {'Content-Type': 'application/x-www-form-urlencoded'},
    },
    params: {
      token: 'df',
    },
    // filter(response) {
    //   return Promise.reject({response, callback: () => $.alert('ffer')})
    //   return response
    // },
    error(ex) {
      if (ex.response) {
        ex.callback = () => {
          $.alert((ex.response?.status || ex) + ': ' + ex.config.url)
        }
      } else {
        console.error(ex)
      }
      return Promise.reject(ex)
    }
  },
  // catchReject(event) {
  //   event.reason.callback && event.reason.callback()
  //   event.preventDefault()
  // },
  
  defaultOptions: {
    'w-grid': {
      gap: 10,
    },
    'w-label': {
      width: 120,
      vertical: !true,
      //suffix: ':'
    },
    'w-buttonbar.oper': {
      gap: 10
    },
    'w-switch': {
      checkedText: "on",
      uncheckedText: "off"
    },
    'w-imageupload': {
      filter: res => res.data,
      on: {
        'click:item'(e, item) {
          let m = $(e).closest('w-imageupload').val()
          let i = m.findIndex(v => v.id == item.id)
          //$.previewImage(m.map(v => v.thumbnail), {current: i})
          $.previewVideo('http://127.0.0.1/test.mp4')
        }
      }
    },
    'w-dialog': {
      width: 980,
      height: 700,
    },
    'w-dialog.medium': {
      width: 653,
      height: 400,
    },
    'w-dialog.large': {
      width: 800,
      height: 600,
    },
    'w-pagebar': {
      height: 50,
      maxHeight: '100%'
    },
    'w-fileupload': {
      fileTypes: '*.jpg;*.gif',
      maxFileSize: '2M',
    }
  }
})

// 添加响应拦截器
// $.http.interceptors.response.use(function(response) {
//   //return Promise.reject({error: true})
//   return response;
// }, function(error) {
//   for(let k in error) console.log(k, error[k])
//   //const { response: { status, statusText, data: { msg = '服务器发生错误' } }} = error
//   //console.log(error.response)
//   return Promise.reject(error)
// })

window.$ = $

const myApp = createApp(App)
//myApp.config.globalProperties.dfish = $

myApp.use(widget).mount('#app')

